<html>
<head><meta charset="utf-8"><title>Span.to across macro exapnsions? · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Span.2Eto.20across.20macro.20exapnsions.3F.html">Span.to across macro exapnsions?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="240969634"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Span.to%20across%20macro%20exapnsions%3F/near/240969634" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Span.2Eto.20across.20macro.20exapnsions.3F.html#240969634">(Jun 01 2021 at 16:41)</a>:</h4>
<p>If I have <code>a + $b</code> and I do <code>a_span.to(b_span)</code> I will get the span of just <code>a</code> because <code>$b</code> comes from a different context. Is there any general solution to this? It seems like I want the expansion <em>child</em> of <code>$b</code>, not the parent, but this is impossible to get?</p>



<a name="240985952"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Span.to%20across%20macro%20exapnsions%3F/near/240985952" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Span.2Eto.20across.20macro.20exapnsions.3F.html#240985952">(Jun 01 2021 at 18:40)</a>:</h4>
<p><span class="user-mention" data-user-id="344914">@Cameron</span> If you're just using the span for diagnostic purposes, you can adjust the <code>SyntaxContext</code> yourself</p>



<a name="241064919"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Span.to%20across%20macro%20exapnsions%3F/near/241064919" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Span.2Eto.20across.20macro.20exapnsions.3F.html#241064919">(Jun 01 2021 at 19:42)</a>:</h4>
<p>But then won't the hi and lo be wrong?</p>



<a name="241069531"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Span.to%20across%20macro%20exapnsions%3F/near/241069531" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Span.2Eto.20across.20macro.20exapnsions.3F.html#241069531">(Jun 01 2021 at 20:18)</a>:</h4>
<p>No - the <code>SyntaxContext</code> is completely independent of the location</p>



<a name="241074031"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Span.to%20across%20macro%20exapnsions%3F/near/241074031" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Span.2Eto.20across.20macro.20exapnsions.3F.html#241074031">(Jun 01 2021 at 20:53)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="fm">macro_rules!</span><span class="w"> </span><span class="n">mac</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="p">(</span><span class="cp">$b</span>:<span class="nc">literal</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="cp">$b</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="n">mac</span><span class="o">!</span><span class="p">(</span><span class="sc">'b'</span><span class="p">);</span><span class="w"></span>
</code></pre></div>
<p>Given the Span of <code>a</code> and <code>'b'</code>from this example, I don't see a way to derive <code>a + $b</code>. <code>Span.to</code> (<a href="https://doc.rust-lang.org/nightly/nightly-rustc/src/rustc_span/lib.rs.html#654-674">source</a>) will just early return the Span of <code>a</code>.</p>



<a name="241075086"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Span.to%20across%20macro%20exapnsions%3F/near/241075086" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Span.2Eto.20across.20macro.20exapnsions.3F.html#241075086">(Jun 01 2021 at 21:01)</a>:</h4>
<p>Or maybe more to the point - how would I get the span of <code>$b</code> within <code>macro_rules!</code>?</p>



<a name="241077166"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Span.to%20across%20macro%20exapnsions%3F/near/241077166" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Span.2Eto.20across.20macro.20exapnsions.3F.html#241077166">(Jun 01 2021 at 21:17)</a>:</h4>
<p><span class="user-mention" data-user-id="360405">@Cameron Steffen</span> You can use <code>Span.with_ctxt</code> to produce a new span with the desired <code>SyntaxContext</code></p>



<a name="241077224"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Span.to%20across%20macro%20exapnsions%3F/near/241077224" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Span.2Eto.20across.20macro.20exapnsions.3F.html#241077224">(Jun 01 2021 at 21:18)</a>:</h4>
<p>Which you can then join with the desired span</p>



<a name="241077288"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Span.to%20across%20macro%20exapnsions%3F/near/241077288" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Span.2Eto.20across.20macro.20exapnsions.3F.html#241077288">(Jun 01 2021 at 21:18)</a>:</h4>
<p>You should only do that if this is intended for a diagnostic message, though. What's your use case?</p>



<a name="241077365"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Span.to%20across%20macro%20exapnsions%3F/near/241077365" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Span.2Eto.20across.20macro.20exapnsions.3F.html#241077365">(Jun 01 2021 at 21:19)</a>:</h4>
<p>Just have wondered about this case a number of times when implementing diagnostics for Clippy</p>



<a name="241077545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Span.to%20across%20macro%20exapnsions%3F/near/241077545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Span.2Eto.20across.20macro.20exapnsions.3F.html#241077545">(Jun 01 2021 at 21:20)</a>:</h4>
<p>Also was wondering what it would take to fix <a href="https://github.com/rust-lang/rust/issues/84122">#84122</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>